package com.servlet;

import com.pojo.Game;
import com.util.BaseServlet;
import com.util.ResultData;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

import static com.util.Vessel.gameSer;

@WebServlet("/game")
public class GameServlet extends BaseServlet {
    @Override
    public Class getServlet() {
        return GameServlet.class;
    }

    public String queryGame(HttpServletRequest request, HttpServletResponse response){
        String name1 = request.getParameter("name");
        String type = request.getParameter("type");
        String company = request.getParameter("company");
        String year = request.getParameter("year");
        String name = "%" + name1 + "%";
        Game game = new Game();
        game.setgName(name);
        game.setgType(type);
        game.setgCompany(company);
        game.setgYear(year);
        List<Game> gameList = gameSer.queryGame(game);
        HttpSession session = request.getSession();
        session.setAttribute("gameList",gameList);
        return "gamesList";
    }

    public ResultData addGame(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("name");
        String type = request.getParameter("type");
        String company = request.getParameter("company");
        String year = request.getParameter("year");
        Game game = new Game();
        game.setgName(name);
        game.setgType(type);
        game.setgCompany(company);
        game.setgYear(year);
        int i= gameSer.haveGame(game);
        int num = 0;
        ResultData resultData = new ResultData();
        if (i>0){
            num = gameSer.addGame(game);
        }else {
            resultData.setMsg("游戏已经存在这个名字");
        }
        try{
            if (num > 0){
                response.sendRedirect("/game?action=queryGame&name=&type=&company=&year=");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return resultData;
    }
}
